<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cells {
    margin-top: 0;
  }
  .weui-cells_form {
    background: #fbfbfb;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label {
    text-align: left;
  }
</style>
<template>
  <div class="comon">
    <div  style="height: 50px; line-height: 50px; background: #f7f7f7; text-align: center; font-size: 16px; border-bottom: 1px solid #bdbbbc; position: relative">
      修改合同价格
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="@/assets/back.png" style="width: 20px" />
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
      <form action="" method="post" id="formid">
        <input type="hidden" name="entity.contractid" :value="id" />
        <input type="hidden" name="entity.departid" :value="baseInfo.departid" />
        <input type="hidden" name="entity.departname" :value="baseInfo.departname" />
        <div class="cellgroup">
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">合同名称</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" readonly :value="baseInfo.name" name="entity.contractname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">批次</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" readonly  type="text"  name="" :value="baseInfo.bitchname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">乙方</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="entity.grower" readonly :value="baseInfo.partbname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">地块</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="" readonly :value="baseInfo.arealandname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">入库库房</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="" readonly :value="baseInfo.srname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">产品</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="entity.productname" readonly :value="baseInfo.productname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">调整前单价(元)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="entity.previousprice" readonly :value="baseInfo.price" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">单价(元)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" name="entity.price">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">调整前扣杂标准</label></div>
            <div class="weui-cell__bd">
              <input type="hidden" name="entity.preIsdiscount" :value="baseInfo.isdiscount" />
              <input class="weui-input" type="text" name="" readonly :value="getIsdiscTypeName(baseInfo.isdiscount)" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">扣杂方式</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="entity.deducttype" v-model="deducttype">
                <option value="1">统一扣杂</option>
                <option value="2">分步扣杂</option>
              </select>
            </div>
          </div>
          <div class="weui-cell" v-if="deducttype == 1">
            <div class="weui-cell__hd"><label class="weui-label">扣杂标准</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="entity.isdiscount" v-model="isdiscount">
                <option value="">请选择</option>
                <option value="1">按照合同约定的扣杂率扣杂</option>
                <option value="2" :disabled="baseInfo.paymentmethod == 1">按照质检结果扣杂</option>
                <option value="3" :disabled="baseInfo.paymentmethod == 1">合同和质检结果取最大扣杂</option>
              </select>
            </div>
          </div>
          <div class="weui-cell" v-if="deducttype == 2">
            <div class="weui-cell__hd"><label class="weui-label">扣杂标准</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="entity.isdiscount" v-model="isdiscount">
                <option value="">请选择</option>
                <option value="4" :disabled="baseInfo.paymentmethod == 1">按质检结果分级扣杂</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">调整前扣杂率(%)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" name="entity.preDiscountrate" readonly :value="baseInfo.discountrate" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">扣杂率(%)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" name="entity.discountrate" ref="discountrate" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">备注</label></div>
            <div class="weui-cell__bd">
              <textarea class="weui-textarea" name="entity.remark" placeholder="请输备注内容" rows="3"></textarea>
            </div>
          </div>
        </div>
        <div class="weui-btn-area">
          <a class="weui-btn weui-btn_primary halfbtn subbtn" href="javascript:" @click="postForm">提交</a>
          <a class="weui-btn weui-btn_primary halfbtn"  href="javascript:" @click="$router.back(-1)" id="sub">返回</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>

  export default {
    name: 'AddEditContractPrice',
    data () {
      return {
        id: '',
        baseInfo: {},
        id: '',
        title: '',
        nodeid: '',
        annex: '',
        isdiscount: '',
        specs: '',
        spec: '',
        deducttype: 1
      }
    },
    mounted () {
      let that = this;

      this.id = this.$route.query.guid;
      this.id = this.$route.query.guid;
      this.nodeid = this.$route.query.nodeid;
      this.annex = this.$route.query.annex;
      this.title = this.$route.query.title;
      this.refuseflag = this.$route.query.refuseflag;

      this.getBaseInfo(this.id);
      this.getSpecs();
    },
    watch: {
      deducttype: function(val) {
        if (val == 2) {
          this.isdiscount = 4;
        } else if (val == 1) {
          this.isdiscount = 1;
        }
      },
    },
    methods: {
      getDtypeName: function(val) {
        let name = '';
        switch (val) {
          case 1:
            name = '统一扣杂';
            break;
          case 2:
            name = '分步扣杂';
            break;
          case 3:
            name = '八三扣杂';
            break;
        }
        return name;
      },
      getQatypeName: function(val) {
        let name = '';
        switch (val) {
          case "1":
            name = '库房质检';
            break;
          case "2":
            name = '地头质检';
            break;
        }
        return name;
      },
      getStypeName: function(val) {
        let name = '';
        switch (val) {
          case "1":
            name = '发货过磅';
            break;
          case "2":
            name = '到货过磅';
            break;
        }
        return name;
      },
      getIsdiscTypeName: function(val) {
        let name = '';
        switch (val) {
          case 1:
            name = '按照合同约定的扣杂率扣杂';
            break;
          case 2:
            name = '按照质检结果扣杂';
            break;
          case 3:
            name = '合同和质检结果取最大扣杂';
            break;
          case 4:
            name = '按质检结果分级扣杂';
            break;
        }
        return name;
      },
      getBaseInfo: function(id) {
        this.axios({
          method: 'get',
          url: '/harvest/Procurementcontract!getDetail.action',
          params: {contractid: id}
        }).then(response => {
          let data = response.data;
          if (data.msg == 'success') {
            this.baseInfo = data.contract;
            this.deducttype = this.baseInfo.deducttype;
            if (this.specs != '') {
              this.spec = this.specs.filter(item => {
                return item.guid == this.baseInfo.specificationid;
              })[0];
            }
          } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
          //不不设置提示
        } else {
          this.toastError('系统错误');
        }
        });
      },
      getSpecs: function() {
        this.axios({
          method: 'get',
          url: '/harvest/Procurementcontract!getSpecifications.action',
          params: {}
        }).then(response => {
          if (response.data.msg == 'success') {
            this.specs = response.data.specs;
            if (this.spec == '' && this.baseInfo != '') {
              this.spec = this.specs.filter(item => {
                return item.guid == this.baseInfo.specificationid;
              })[0];
            }
          } else {
            this.toastText('获取产品数据失败')
          }
        });
      },
      postForm: function () {
        let that = this;

        if (!this.checkEmy('price')) {
          this.toastText('请输入单价！');
          return false;
        }
        //防止重复提交
        $('.subbtn').attr("disabled",true).css("pointer-events","none");
        //显示提交状态
        $.showLoading("正在提交");

        let postData = this.serialize2Obj($("#formid").serializeArray());
        postData.nodeid = this.nodeid;

        this.axios({
          method: 'post',
          url: '/harvest/EditContractprice!save.action',
          params: postData
        }).then(response => {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();

          if (response.data.msg == 'success') {
            this.toastSuccess('操作成功!');
            this.$router.push({name: 'ChangeContractPriceList'});
          } else if (response.data.msg == 'canotopt') {
            this.toastText('不允许的操作！');
          } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
          //不不设置提示
        } else {
          this.toastError('系统错误');
        }
        }).catch(function (error) {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();
          that.toastError('系统错误');
        });
      }
    }
  }
</script>
